package it.polimi.swKnights.SWIMv2.ejb.entityBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

/**
 * Entity implementation class for Entity: RegisteredUser
 * 
 */
@Entity
public class RegisteredUser extends User implements Serializable
{

	private boolean isPublic;
	
	private String image; // image name

	/*
	 * Relationships
	 */

	@ManyToMany//(cascade =  CascadeType.REMOVE)  //not ok
	@JoinTable(name = "HaveAbilities", joinColumns = { @JoinColumn(name = "username") }, inverseJoinColumns = { @JoinColumn(name = "name") })
	private Set<Ability> abilities;

	@OneToMany(mappedBy = "requested", cascade = CascadeType.ALL) //no cascade 
	private Set<FriendshipRequest> friendshipRequests;

	@OneToMany(mappedBy = "user1", cascade = CascadeType.REMOVE) //ok
	private Set<Friendship> addedFriends;

	@OneToMany(mappedBy = "user2", cascade = CascadeType.REMOVE) //ok
	private Set<Friendship> addedByFriends;

	@OneToMany(mappedBy = "recipientUser"/*, cascade = CascadeType.ALL*/)  //not ok cascade
	private List<ReceivedMessages> receivedMessages;

	/*
	 * Serial Version ID
	 */

	private static final long serialVersionUID = 1L;

	/*
	 * Constructors
	 */

	public RegisteredUser()
	{
		super();
	}

	public RegisteredUser(String userName, String passwordHash, String eMail)
	{
		super(userName, passwordHash, eMail);
	}

	/*
	 * Getters and setters
	 */

	public String getImage()
	{
		return image;
	}

	public void setImage(String image)
	{
		this.image = image;
	}
	
	public boolean isPublic()
	{
		return isPublic;
	}

	public void setPublic(boolean isPublic)
	{
		this.isPublic = isPublic;
	}

	public /* Giurato */  void removeAllAbilities()
	{
		
		this.abilities.clear();
	}
	
	public List<RegisteredUser> getFriends()
	{
		List<RegisteredUser> friendList = new ArrayList<RegisteredUser>();

		for (Friendship friendship : this.addedByFriends)
		{
			friendList.add(friendship.getUser1());
		}

		for (Friendship friendship : this.addedFriends)
		{
			friendList.add(friendship.getUser2());
		}

		return friendList;
	}
	
	public List<ReceivedMessages> getReceivedMessages()
	{
		return this.receivedMessages;
		
	}
	
	public Set<FriendshipRequest>  getFriendshipRequests()
	{
		return this.friendshipRequests;
	}
	
	
	public void addAbility(Ability ability)
	{
		this.abilities.add(ability);
	}
	
	public void removeAbility(Ability ability)
	{
		this.abilities.remove(ability);
	}

	public Set<Ability> getAbilities()
	{
		Set<Ability> abilities = new HashSet<Ability>();
		abilities.addAll(this.abilities);
		return abilities;
	}

	@Override
	public String toString()
	{

		return " name = " + this.getName() + " surname =  " + this.getSurname()
				+ " username = " + this.getUserName();
	}

}
